package avatar

import (
	"context"
	v1 "goGameWebServer/api/avatar/v1"
	"goGameWebServer/internal/common/utils"
	"goGameWebServer/internal/controller"
	"goGameWebServer/internal/defines/cerror"
	"goGameWebServer/internal/defines/entityName"
	"goGameWebServer/internal/defines/platform"
	"goGameWebServer/internal/manager/cachemanager"
	"goGameWebServer/internal/manager/platformmanager"
	"goGameWebServer/internal/model/entity"
	"goGameWebServer/internal/register"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
)

func (c *AvatarControllerPrivateV1) CreateAvatar(ctx context.Context, req *v1.AvatarCreateReq) (res *v1.AvatarCreateRes, err error) {
	r := g.RequestFromCtx(ctx)
	uName := r.GetParam("username").String()
	ac := register.GetModel(entityName.EntityName_Account)
	acD := ac.GetEntityData().(*entity.AccountModelData)
	acD.AccountName = uName
	ac.LoadEntityDataFromDB(nil)
	if acD.AvatarId != 0 {
		controller.BackResponseError(r, cerror.AVATAR_ALREADY_EXIST_ERROR, cerror.GetErrMsg(cerror.AVATAR_ALREADY_EXIST_ERROR))
		return
	}
	avatarM := register.GetModel(entityName.EntityName_Avatar)
	avatarD := avatarM.GetEntityData().(*entity.AvatarModelData)
	avatarD.Name = req.AvatarName
	avatarD.Data = req.Data
	e := avatarM.LoadEntityDataFromDB(nil)
	if e != nil {
		controller.BackResponseError(r, cerror.AVATAR_CREATE_ERROR, cerror.GetErrMsg(cerror.AVATAR_CREATE_ERROR))
		return
	}
	res = &v1.AvatarCreateRes{}
	acD.AvatarId = avatarD.Id
	ac.SaveEntityDataToDB()
	res.Id = avatarD.Id
	res.Name = avatarD.Name
	res.Data = avatarD.Data
	res.UseServer = avatarD.UseServer
	res.CreateTime = utils.GetTimestampSecond(avatarD.CreateTime)
	controller.BackResponseOK(r, res)
	return
}

func (c *AvatarControllerPrivateV1) QueryAvatar(ctx context.Context, req *v1.AvatarQueryReq) (res *v1.AvatarQueryRes, err error) {
	r := g.RequestFromCtx(ctx)
	uName := r.GetParam("username").String()
	ac := register.GetModel(entityName.EntityName_Account)
	acD := ac.GetEntityData().(*entity.AccountModelData)
	acD.AccountName = uName
	ac.LoadEntityDataFromDB(nil)
	if acD.AvatarId == 0 {
		controller.BackResponseError(r, cerror.AVATAR_NOT_FOUND_ERROR, cerror.GetErrMsg(cerror.AVATAR_NOT_FOUND_ERROR))
		return
	}
	avatarM := register.GetModel(entityName.EntityName_Avatar)
	avatarD := avatarM.GetEntityData().(*entity.AvatarModelData)
	avatarD.Id = acD.AvatarId
	e := avatarM.LoadEntityDataFromDB(nil)
	if e != nil {
		controller.BackResponseError(r, cerror.AVATAR_QUERY_ERROR, cerror.GetErrMsg(cerror.AVATAR_QUERY_ERROR))
		return
	}
	res = &v1.AvatarQueryRes{}
	res.Id = avatarD.Id
	res.Name = avatarD.Name
	res.UseServer = avatarD.UseServer
	res.CreateTime = utils.GetTimestampSecond(avatarD.CreateTime)
	res.Data = avatarD.Data
	controller.BackResponseOK(r, res)
	return
}

func (c *AvatarControllerPrivateV1) UpdateAvatar(ctx context.Context, req *v1.AvatarUpdateReq) (res *v1.AvatarUpdateRes, err error) {
	avatarM := register.GetModel(entityName.EntityName_Avatar)
	avatarD := avatarM.GetEntityData().(*entity.AvatarModelData)
	avatarD.Id = req.AvatarId
	e := avatarM.LoadEntityDataFromDB(nil)
	if e != nil {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.AVATAR_QUERY_ERROR, cerror.GetErrMsg(cerror.AVATAR_QUERY_ERROR))
		return
	}
	avatarD.Data = req.Data
	e = avatarM.SaveEntityDataToDB()
	if e != nil {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.AVATAR_UPDATE_ERROR, cerror.GetErrMsg(cerror.AVATAR_UPDATE_ERROR))
		return
	}
	res = &v1.AvatarUpdateRes{}
	res.UpdateTime = utils.GetNowTimestampSecond()
	controller.BackResponseOK(g.RequestFromCtx(ctx), res)
	return
}

func (c *AvatarControllerPrivateV1) UpdateAvatarModule(ctx context.Context, req *v1.AvatarModuleUpdateReq) (res *v1.AvatarModuleRes, err error) {
	amm := register.GetModel(entityName.EntityName_AvatarModuleEntity)
	ammd := amm.GetEntityData().(*entity.AvatarModuleEntityModelData)
	ammd.AvatarId = req.AvatarId
	ammd.ModuleId = req.ModuleId
	e := amm.LoadEntityDataFromDB(nil)
	if e != nil {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.AVATAR_MODULE_QUERY_ERROR, cerror.GetErrMsg(cerror.AVATAR_MODULE_QUERY_ERROR))
		return
	}
	ammd.Data = req.Data
	e = amm.SaveEntityDataToDB()
	if e != nil {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.AVATAR_MODULE_UPDATE_ERROR, cerror.GetErrMsg(cerror.AVATAR_MODULE_UPDATE_ERROR))
		return
	}
	res = &v1.AvatarModuleRes{}
	res.AvatarId = req.AvatarId
	res.ModuleId = req.ModuleId
	res.Data = ammd.Data
	controller.BackResponseOK(g.RequestFromCtx(ctx), res)
	return
}

func (c *AvatarControllerPrivateV1) QueryAvatarModule(ctx context.Context, req *v1.AvatarModuleQueryReq) (res *v1.AvatarModuleRes, err error) {
	amm := register.GetModel(entityName.EntityName_AvatarModuleEntity)
	ammd := amm.GetEntityData().(*entity.AvatarModuleEntityModelData)
	ammd.AvatarId = req.AvatarId
	ammd.ModuleId = req.ModuleId
	e := amm.LoadEntityDataFromDB(nil)
	if e != nil {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.AVATAR_MODULE_QUERY_ERROR, cerror.GetErrMsg(cerror.AVATAR_MODULE_QUERY_ERROR))
		return
	}
	res = &v1.AvatarModuleRes{}
	res.AvatarId = req.AvatarId
	res.ModuleId = req.ModuleId
	res.Data = ammd.Data
	controller.BackResponseOK(g.RequestFromCtx(ctx), res)
	return
}

func (c *AvatarControllerPrivateV1) QueryAvatarByteJumpEcpm(ctx context.Context, req *v1.AvatarQueryByteJumpEcpmReq) (res *v1.AvatarQueryByteJumpEcpmRes, err error) {
	r := g.RequestFromCtx(ctx)
	uName := r.GetParam("username").String()
	reqParams := make(map[string]interface{})
	reqParams["openId"] = uName
	platformOpt := platformmanager.ManagerPlatform().GetPlatform(platform.ByteJumpGame)
	if platformOpt == nil {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.HTTP_PLATFORM_TYPE_ERROR, cerror.GetErrMsg(cerror.HTTP_PLATFORM_TYPE_ERROR))
		return
	}
	resBack, e := platformOpt.Ecpm(reqParams, platformmanager.ManagerPlatform().GetPlatformOption(req.AppId))
	if e != nil {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.HTTP_PLATFORM_ECPM_ERROR, cerror.GetErrMsg(cerror.HTTP_PLATFORM_ECPM_ERROR))
		return
	}
	res = &v1.AvatarQueryByteJumpEcpmRes{}
	res.Cost = gconv.Float64(resBack["cost"])
	res.Total = gconv.Int(resBack["total"])
	controller.BackResponseOK(g.RequestFromCtx(ctx), res)
	return
}

func (c *AvatarControllerPrivateV1) StoreSet(ctx context.Context, req *v1.AvatarStoreSetReq) (res *v1.AvatarStoreSetRes, err error) {
	_, e := cachemanager.ManagerCache().StoreListPush(req.StoreKey, req.StoreValue, req.StoreLength, req.StoreExpire)
	if e != nil {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.STORE_SET_ERROR, cerror.GetErrMsg(cerror.STORE_SET_ERROR)+e.Error())
		return
	}
	controller.BackResponseOK(g.RequestFromCtx(ctx), "ok")
	return
}

func (c *AvatarControllerPrivateV1) StoreGet(ctx context.Context, req *v1.AvatarStoreGetReq) (res *v1.AvatarStoreGetRes, err error) {
	values, e := cachemanager.ManagerCache().StoreListGet(req.StoreKey)
	if e != nil {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.STORE_GET_ERROR, cerror.GetErrMsg(cerror.STORE_GET_ERROR)+e.Error())
		return
	}
	controller.BackResponseOK(g.RequestFromCtx(ctx), values)
	return
}

func (c *AvatarControllerPrivateV1) StoreDel(ctx context.Context, req *v1.AvatarStoreDelReq) (res *v1.AvatarStoreDelRes, err error) {
	for i := 0; i < len(req.StoreValue); i++ {
		cachemanager.ManagerCache().StoreListDel(req.StoreKey, req.StoreValue[i])
	}
	controller.BackResponseOK(g.RequestFromCtx(ctx), "ok")
	return
}

func (c *AvatarControllerPrivateV1) WxQRCode(ctx context.Context, req *v1.AvatarQRCodeReq) (res *v1.AvatarQRCodeRes, err error) {
	reqParams := make(map[string]interface{})
	reqParams["scene"] = req.Scene
	reqParams["version"] = req.EnvVersion
	reqParams["width"] = req.Width

	platformOpt := platformmanager.ManagerPlatform().GetPlatform(platform.WeChatGame)
	if platformOpt == nil {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.HTTP_PLATFORM_TYPE_ERROR, cerror.GetErrMsg(cerror.HTTP_PLATFORM_TYPE_ERROR))
		return
	}
	reBack, e := platformOpt.QRCodeUnlimited(reqParams, platformmanager.ManagerPlatform().GetPlatformOption(req.AppId))
	if e != nil {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.HTTP_PLATFORM_QRCODE_ERROR, cerror.GetErrMsg(cerror.HTTP_PLATFORM_QRCODE_ERROR))
		return
	}
	if !reBack["success"].(bool) {
		controller.BackResponseError(g.RequestFromCtx(ctx), cerror.HTTP_PLATFORM_QRCODE_ERROR, reBack["errmsg"].(string))
		return
	}
	res = &v1.AvatarQRCodeRes{}
	res.Data = reBack["data"].([]byte)
	controller.BackResponseOK(g.RequestFromCtx(ctx), res)
	return
}

func (c *AvatarControllerPrivateV1) Subscribe(ctx context.Context, req *v1.AvatarSubscribeReq) (res *v1.AvatarSubscribeRes, err error) {
	r := g.RequestFromCtx(ctx)
	uName := r.GetParam("username").String()
	ac := register.GetModel(entityName.EntityName_Account)
	acD := ac.GetEntityData().(*entity.AccountModelData)
	acD.AccountName = uName
	ac.LoadEntityDataFromDB(nil)
	// 查询订阅信息,没有就创建
	sb := register.GetModel(entityName.EntityName_Subscribe)
	sbd := sb.GetEntityData().(*entity.SubscribeModelData)
	sbd.AvatarId = req.AvatarId
	sbd.SubscribeType = req.SubscribeType
	sb.LoadEntityDataFromDB(nil)
	sbd.AccountName = uName
	sbd.Status = 1
	sbd.SendTime = req.SendTime
	sb.SaveEntityDataToDB()
	controller.BackResponseOK(r, "ok")
	return
}
